<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      * {
        margin: 0;
        padding: 0;
      }
      .container {
        width: 400px;
        height: 250px;
        border: 1px solid #000;
        margin: 50px auto;
        overflow: hidden;
      }

      ul {
        width: 2000px;
        height: 250px;
        background-color: lightblue;
        display: flex;
      }

      .active {
        transition: all 0.4s;
      }

      li {
        width: 400px;
        height: 250px;
      }

      img {
        width: 100%;
        height: 100%;
      }
    </style>
  </head>
  <body>
    <div id="app">
      <div class="container" @mouseover="stop" @mouseleave="restart">
        <ul
          :style="{transform: 'translateX(' + -400 * i + 'px)'}"
          :class="{active: active}"
        >
          <li v-for="(item, index) in imgArr" :key="index">
            <img :src="item" alt="" />
          </li>
        </ul>
      </div>

      <button>&lt;</button>
      <button @click="rightClick">&gt;</button>
    </div>

    <script src="../Vue.js"></script>
    <script>
      let timer;
      new Vue({
        el: "#app",
        data: {
          imgArr: [
            "https://img.alicdn.com/tfs/TB1FjZ7VWL7gK0jSZFBXXXZZpXa-520-280.png",
            "https://gtms03.alicdn.com/tps/i3/TB1gXd1JXXXXXapXpXXvKyzTVXX-520-280.jpg",
            "https://gtms01.alicdn.com/tps/i1/TB1r4h8JXXXXXXoXXXXvKyzTVXX-520-280.jpg",
            "https://gtms02.alicdn.com/tps/i2/TB10vPXKpXXXXacXXXXvKyzTVXX-520-280.jpg",
            "https://img.alicdn.com/tfs/TB1FjZ7VWL7gK0jSZFBXXXZZpXa-520-280.png",
          ],
          i: 0,
          active: true,
          finish: true,
        },
        methods: {
          // 只能当动画结束以后才能让点击
          rightClick() {
            if (this.finish) {
              this.active = true;
              this.finish = false;
              this.i++;
              setTimeout(() => {
                // 等待动画结束
                if (this.i === 4) {
                  this.finish = true;
                  this.active = false;
                  // 已经是最后一张了
                  this.i = 0;
                }
              }, 400);
            }
          },
          stop() {
            clearInterval(timer);
          },
          restart() {
            timer = setInterval(() => {
              this.rightClick();
            }, 3000);
          },
        },
        mounted() {
          timer = setInterval(() => {
            this.rightClick();
          }, 3000);
        },
      });
    </script>
  </body>
</html>
